/**
 * Created by Administrator on 2017/1/17 0017.
 */
$(function () {
    $(".member_form").click(function () {
        $('#loadingToast').fadeIn();
        var _this = $("#form");
        $.ajax({
            type : _this.attr('method'),
            url : _this.attr('action'),
            data : _this.serialize(),
            dataType : 'json',
            success : function (msg) {
                $('#loadingToast').fadeOut();
                if (msg.code == 200) {
                    //alert('操作成功');
                    if (msg.url) {
                        window.location.href = msg.url;
                    } else {
                        window.location.href = '/wechat/member/index';
                    }
                } else {
                    var code = msg.code;
                    $.alert(error_code[code]);
                }
            }
        });
    });

    // 检查用户名是否存在
    $('input[name="phone"]').bind('input propertychange', function() {
        var _this = $(this);
        var phone = _this.val();
        if (phone.length == 0) {
            $(".referrer").text('');
            return false;
        }
        if (phone.length != 11) {
            $(".referrer").text('请输入正确的手机号码！');
            $('.js-verify').addClass('no-phone').attr('disabled','disabled');
            return false;
        }
        $(".referrer").text('');

        $('#loadingToast').fadeIn();
        $.get("/wechat/login/check_phone", {phone : phone}, function (msg) {
            $('#loadingToast').fadeOut();
            if ($(".login_form").length > 0) {
                var type = $('input[name="type"]').val();
                if (!msg.data && type != 2) {
                    $(".referrer").text('用户名不存在!');
                } else {
                    $('.js-verify').removeClass('no-phone').removeAttr('disabled');
                }
            }
            if ($(".register_form").length > 0) {
                if (msg.data) {
                    $(".referrer").text('用户名已存在!');
                } else {
                    $('.js-verify').removeClass('no-phone').removeAttr('disabled');
                }
            }
        }, 'json');
    });
});